package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeGoodsJointActivity;
import com.github.postsanf.yinian.bean.YNShopCar;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class ShopCarOrderListAdapter3 extends BaseAdapter {
    private List<YNShopCar> listItems;
    private LayoutInflater layoutinflater;
    private Context context;

    static class ViewHolder {
        private LinearLayout shopcar_order_item;
        private ImageView image;
        private TextView name;
        private TextView enname;
        private TextView num;
        private TextView price;
        private TextView shopcar_item_bline;
    }

    public ShopCarOrderListAdapter3(Context context, List<YNShopCar> dataSet) {
        this.listItems = dataSet;
        this.context = context;
        this.layoutinflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final YNShopCar ynShopCar = listItems.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutinflater.inflate(R.layout.shopcarorderlist_listitem, null);
            holder.shopcar_order_item = (LinearLayout) convertView.findViewById(R.id.shopcar_order_item);
            holder.image = (ImageView) convertView.findViewById(R.id.shopcar_item_image);
            holder.name = (TextView) convertView.findViewById(R.id.shopcar_item_name);
            holder.enname = (TextView) convertView.findViewById(R.id.shopcar_item_enname);
            holder.num = (TextView) convertView.findViewById(R.id.shopcar_item_num);
            holder.price = (TextView) convertView.findViewById(R.id.shopcar_item_price);
            holder.shopcar_item_bline = (TextView) convertView.findViewById(R.id.shopcar_item_bline);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageOptHelper.showFrescoStatus(ynShopCar.getEbGoodsPic(), holder.image, context);
        holder.shopcar_order_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, TimeGoodsJointActivity.class).putExtra("itemID",String.valueOf(ynShopCar.getCartItemID())));
            }
        });
        holder.name.setText(ynShopCar.getEbGoodsName());
        holder.enname.setText(ynShopCar.getEbGoodsEnName());
        holder.price.setText("¥" + ynShopCar.getNowPrice());
        holder.num.setText("数量: "+String.valueOf(ynShopCar.getCartItemNum()));
        if (position == listItems.size() - 1) {
            holder.shopcar_item_bline.setVisibility(View.GONE);
        } else {
            holder.shopcar_item_bline.setVisibility(View.VISIBLE);
        }
        return convertView;
    }


}